﻿function NotificationBox() {

    $("#notificationBox .menu > li").click(function (e) {
        
        switch (e.target.id) {
            case "friendNotifications":
                //change status & style menu
                $("#friendNotifications").addClass("active");
                $("#friendNotifications").removeClass("notification");
                $("#courseNotifications").removeClass("active");
                $("#otherNotifications").removeClass("active");
                //display selected division, hide others
                $("div.friendNotifications").fadeIn();
                $("div.courseNotifications").css("display", "none");
                $("div.otherNotifications").css("display", "none");
                $(".friendNotifications").find(".vscrollbar").tinyscrollbar_update();
                break;
            case "courseNotifications":
                //change status & style menu
                $("#friendNotifications").removeClass("active");
                $("#courseNotifications").addClass("active");
                $("#courseNotifications").removeClass("notification");
                $("#otherNotifications").removeClass("active");
                //display selected division, hide others
                $("div.courseNotifications").fadeIn();
                $("div.friendNotifications").css("display", "none");
                $("div.otherNotifications").css("display", "none");
                $(".courseNotifications").find(".vscrollbar").tinyscrollbar_update();
                break;
            case "otherNotifications":
                //change status & style menu
                $("#friendNotifications").removeClass("active");
                $("#courseNotifications").removeClass("active");
                $("#otherNotifications").removeClass("notification");
                $("#otherNotifications").addClass("active");
                //display selected division, hide others
                $("div.otherNotifications").fadeIn();
                $("div.friendNotifications").css("display", "none");
                $("div.courseNotifications").css("display", "none");
                $(".otherNotifications").find(".vscrollbar").tinyscrollbar_update();
                break;
        }
        //alert(e.target.id);
        return false;
    });
}
$(document).ready(function () {
    $("li[NotificationID]").live({
        'mouseenter': function () {
            $(this).find("a[name=checkNotification]").css('display', 'block');
        },
        'mouseleave': function () {
            $(this).find("a[name=checkNotification]").css('display', 'none');
        }
    });
    $("li[NotificationID]").live('click', function (event) {
        if ($(event.target).parent().attr("name") != "checkNotification") {
            var $this = $(this);
            var notificationID = $(this).attr("NotificationID");
            var typeNotification = $(this).attr("typeNotification");
            HasReadNotification(notificationID, typeNotification, $this);
        }
    })
    //delete notification
    $("a[name=checkNotification]").live('click', function (event) {
        var record = $(this).parents('li');
        var notificationID = record.attr("NotificationID");
        var typeNotification = record.attr("typeNotification");
        DeleteNotification(notificationID, typeNotification, record);

    })
    //$(document).delegate("a[name=checkNotification]", 'click', function () { });
});
function HasReadNotification(notificationID, type, $this) {
    var item = $($this);
    $.post("/Notification/HasReadNotification", { 'notificationID': notificationID, 'type': type }, function (data) {
        if (data == "success") {
            item.css('background-color', "#FFFFFF");
        }
        else {
            alert("Có lỗi, refresh trình duyệt và thử lại sau");
        }
    })

}
function DeleteNotification(notificationID, type, record) {
    var item = record;
    $.post("/Notification/DeleteNotification", { 'notificationID': notificationID, 'type': type }, function (data) {
        if (data == "success") {
            item.remove();
        }
        else {
            alert("Có lỗi, refresh trình duyệt và thử lại sau");
        }
    })
}

function BoxAcceptFriendRelation(friendID, $this) {
    $($this).fadeOut();
    $.post('/Friend/AcceptFriendRequest', { 'friendID': friendID }, function (data) {
        if (data == "success") {
            alert("Kết bạn thành công");
            $($this).parents("li").remove();
            window.location.reload();
        }
        else {
            $($this).fadeIn();
            alert("Có lỗi, vui lòng thử lại sau");
        }
    });
    return false;
}